{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Maximum Profit From Stocks\n",
    "\n",
    "[The Original Question](https://mp.weixin.qq.com/s/JgCBBcm4E_wMUba5cXI9kg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "You are given an array. Each element represents the price of a stock on that particular day. Calculate and return the maximum profit you can make from buying and selling that stock only once."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "```python3\n",
    "[9, 11, 8, 5, 7, 10]\n",
    "```\n",
    "\n",
    "Here, the optimal trade is to buy when the price is `5`, and sell when it is `10`, so the return value should be $10 - 5 = 5$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def buy_and_sell(array: list) -> int:\n",
    "    max_prof = i = 0\n",
    "    j = 1\n",
    "    while i < len(array) and j < len(array):\n",
    "        if array[j] > array[j - 1]:\n",
    "            max_prof = max(array[j] - array[i], max_prof)\n",
    "            j += 1\n",
    "            pass\n",
    "        else:\n",
    "            i = j\n",
    "            j += 1\n",
    "            pass\n",
    "        pass\n",
    "    return max_prof"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "print(buy_and_sell([9, 11, 8, 5, 7, 10]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
